package com.oracle.servlet;

import com.oracle.entity.User;
import com.oracle.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        String username = req.getParameter("username");
        String password = req.getParameter("password");
        UserService userService = new UserService();
        User register = userService.register(username, password);
        if(register==null){
            req.getRequestDispatcher("/success.html").forward(req,resp);
        }else {
            PrintWriter writer=resp.getWriter();
            writer.write("用户名存在!<br/>");
            writer.close();
        }

    }
}
